package org.user.controller;

import com.fasterxml.jackson.core.JsonProcessingException;

import org.common.util.RedisCommon;
import org.common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.user.domain.pojo.UserLogin;
import org.user.domain.pojo.UserRegister;
import org.user.service.UserService;

import static org.user.util.StaticVariable.Phone_Code;


@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisCommon redisCommon;

    @PostMapping("/register")
    public Result userRegister(@RequestBody UserRegister userRegister){
        if(redisCommon.getValueOfString(Phone_Code+userRegister.getPhone()).equals(userRegister.getCode())){
            if(userRegister.getPassword().equals(userRegister.getPassword2())){
                if(userService.getUserLoginByPhone(userRegister.getPhone())==null){
                    userService.register(userRegister);
                }else {
                    return Result.error("账号已存在");
                }
            }else {
                return Result.error("两次密码不一致");
            }
        }else {
            return Result.error("验证码错误");
        }
        return Result.success();
    }

    @PostMapping("/login")
    //用户登录
    public Result userLogin(@RequestBody UserLogin userLogin) throws JsonProcessingException {
        return userService.login(userLogin);
    }

    @PostMapping("/loginOut")
    public Result userLoginOut(){
        return Result.success("退出成功");
    }

    @PostMapping("/getUserInfo")
    public Result getUserInfo(){
        return Result.success(userService.getUserLoginByPhone("15279736013"));
    }

    @GetMapping("/getAllExhibitionReservation")
    public Result getAllExhibitionReservation(){
        return Result.success();
    }



}
